package cn.sinata.wanglin.neighbor.activity;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.RecyclerListActivity;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.view.SwipeRefreshRecyclerLayout;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cn.sinata.wanglin.adapter.SelectUserAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.db.model.User;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

public class InviteMemberActivity extends RecyclerListActivity {

    private String id;
    private String account;
    private String userId;
    private SelectUserAdapter selectUserAdapter;
    private ArrayList<User> mUsers = new ArrayList<>();

    @Override
    protected void initView() {
        super.initView();
        setTitle("邀请成员");
        id = getIntent().getStringExtra("id");
        account = getIntent().getStringExtra("account");
        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
        addRightButton("邀请", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (getSelectUser().size() == 0) {
                    showToast("请先选择邀请的好友！");
                    return;
                }
                JSONArray array = new JSONArray(getSelectUser());
                showDialog();
                invite(array.toString());
            }
        });

        showDialog();
        inviteMember();
    }

    @Override
    protected SwipeRefreshRecyclerLayout.Mode getMode() {
        return SwipeRefreshRecyclerLayout.Mode.Top;
    }

    @Override
    protected RecyclerView.Adapter setAdapter() {
        selectUserAdapter = new SelectUserAdapter(mUsers);
        return selectUserAdapter;
    }

    void inviteMember(){
        HttpManager.getInviteUser(userId,id)
                .subscribe(new ResultDataSubscriber<ArrayList<User>>(this) {
                    @Override
                    public void onSuccess(String msg, ArrayList<User> users) {
                        mUsers.clear();
                        if (users.size() == 0) {
                            setLoadMoreText("没有可邀请好友");
                        }
                        mUsers.addAll(users);
                        selectUserAdapter.notifyDataSetChanged();
                    }
                });
    }

    private ArrayList<Map<String,String>> getSelectUser(){
        ArrayList<Map<String,String>> list = new ArrayList<>();
        for (User mUser : mUsers) {
            if (mUser.isSelect()) {
                Map<String ,String> map = new HashMap<>();
                map.put("userID",mUser.getUserID());
                map.put("account",mUser.getAccount());
                map.put("groupNickName",mUser.getName());
                list.add(map);
            }
        }
        return list;
    }

    void invite(String members){
        HttpManager.inviteMember(id,account,members)
                .subscribe(new ResultDataSubscriber<JsonObject>(this) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showToast(msg);
                        setResult(RESULT_OK);
                        finish();
                    }
                });
    }
}
